$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/replenishment/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '补货单号', name: 'number', index: 'number', width: 80 }, 			
			{ label: '货架', name: 'shelvesid', index: 'shelvesId', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.shelvesid!=null&&rowdata.shelvesid!=""){
                        var shelvesname = "";

                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/goodshelves/info/"+rowdata.shelvesid,
                            success: function(r){
                                shelvesname =r.goodshelves.shelvesname;

                            }
                        });

                        return shelvesname;
                    }else{
                        return '';
                    }


                } },
			{ label: '提交人', name: 'userid', index: 'userId', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.userid!=null&&rowdata.userid!=""){
                        var username = "";

                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/user/info/"+rowdata.userid,
                            success: function(r){
                                username =r.user.username;


                            }
                        });

                        return username;
                    }else{
                        return '';
                    }


                } },
			{ label: '提交时间', name: 'createtime', index: 'createtime', width: 80 }, 			
			{ label: '商品种类', name: 'count', index: 'count', width: 80 },
			{ label: '留言', name: 'message', index: 'message', width: 100 },
			{ label: '状态', name: 'statu', index: 'statu', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '待处理';
                    }
                    if (item === 1) {
                        return '已处理';
                    }
                    if (item === 2) {
                        return '已拒绝';
                    }
                } },
            { label: '操作', name: 'sy', width: 90,formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.add("+ rowdata.id +")'>" +
                        "详情</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.queryGoodshelves();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            number:null,
            shelvesid:null,
            username:null,
            statu:null,
            startime:null,
            endtime:null,
            userid:null
		},
		showList: true,
		title: null,
		replenishment: {},
        onshelves: {},
        childProduct:[],
        childShelves:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(id){
			vm.showList = false;
			vm.title = "补货申请详情";
			vm.replenishment = {};
            vm.getInfo(id);
            $("#number").text(vm.replenishment.number);
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/user/info/"+vm.replenishment.userid,
                success: function(r){
                    $("#tjuser").text(r.user.username);
                }
            });
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/goodshelves/info/"+vm.replenishment.shelvesid,
                success: function(r){
                    var obj = {};
                    obj.id = r.goodshelves.id;
                    obj.name = r.goodshelves.shelvesname;
                    vm.childShelves.push(obj);
                    $("#shelvesname").text(r.goodshelves.shelvesname);
                }
            });

            $("#createtime").text(vm.replenishment.createtime);
            var statustr = "";
            if (vm.replenishment.statu == 0) {
                statustr = '待处理';
            }
            if (vm.replenishment.statu == 1) {
                statustr = '已处理';
            }
            if (vm.replenishment.statu == 2) {
                statustr = '已拒绝';
            }
            $("#statu").text(statustr);
            $("#message").text(vm.replenishment.message);

            $("#infoproduct").html("");
            var temp = "";
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/replenishmentitem/info/"+id,
                success: function(r){

                    for(var i=0;i<r.product.length;i++){
                        var obj = {};
                        obj.id = r.product[i].id;
                        obj.name = r.product[i].productname;
                        obj.headimg = r.product[i].productimg;
                        obj.costprice = r.product[i].costprice;
                        obj.sellprice = r.product[i].sellprice;
                        obj.count = 1;
                        vm.childProduct.push(obj);
                        temp +="<tr style='height:39px;' ><td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);'>"+(i+1)+"</td>" +
                            "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'><img src='"+r.product[i].productimg+"'></td>" +
                            "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+r.product[i].productname+"</td>" +
                            "<td style='text-align: center;background-color: #fff!important;border-top:solid 1px rgb(226, 229, 237);border-left:solid 1px rgb(226, 229, 237);'>"+r.product[i].barcode+"</td></tr>";
                    }
                    $("#spsl").text(r.product.length);
                }
            });

            $("#infoproduct").html(temp);
            if(vm.replenishment.statu == 0){
                $("#tjan").show();
            }else{
                $("#tjan").hide();
            }
		},
        process: function () {
            var bc = $("input[name='bh']:checked").val();
            if(bc == 0){
                $("#jjyy").hide();
            }
            if(bc == 1){
                $("#jjyy").show();
            }
        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            var bc = $("input[name='bh']:checked").val();
            if(bc == 0){
                vm.onshelves.type = 1;
            }
            if(bc == 1){
                vm.onshelves.type = 2;
            }
            vm.onshelves.ids = vm.replenishment.id;
            vm.onshelves.statu = 0;
            vm.onshelves.products = vm.childProduct;
            vm.onshelves.shelvesid = vm.childShelves;
			$.ajax({
				type: "POST",
			    url: baseURL + "sys/replenishment/save",
                contentType: "application/json",
			    data: JSON.stringify(vm.onshelves),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
							vm.childProduct = [];
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/replenishment/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/replenishment/info/"+id,
                success: function(r){
                    vm.replenishment = r.replenishment;
                }
            });
		},
		reload: function (event) {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            if((startime!=''&&typeof startime!='undefined')&&(endtime!=''&&typeof endtime!='undefined')){
                var st = new Date(startime).getTime();
                var et = new Date(endtime).getTime();
                if(st>et){
                    alert("开始时间不能大于结束时间");
                    return;
                }
            }
			vm.showList = true;
            if(endtime!=''&&typeof endtime!='undefined'){
                var date = new Date(new Date(endtime).getTime()+(24*60*60*1000));
                var premonth   = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
                if(premonth<10){
                    premonth = "0"+premonth;
                }
                var preday = date.getDate(); //获取当前日(1-31)
                if(preday<10){
                    preday = "0"+preday;
                }
                endtime = date.getFullYear()+"-"+premonth+"-"+preday;
            }
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'shelvesid':vm.q.shelvesid,'userid':vm.q.userid,'number':vm.q.number,'statu':vm.q.statu,'startime':startime,'endtime':endtime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});